﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RIS.TMP.WEB.APP.Models;
using RIS.TMP.WEB.APP.AddMigrations;

namespace RIS.TMP.WEB.APP.Controllers
{
    public class UserLoginController : Controller
    {
        private AllContext db = new AllContext();

        //
        // GET: /UserLogin/

        public ActionResult Index()
        {
            var userlogins = db.UserLogins.Include(u => u.CFEmployee).Where(x => x.RecordStatus == "U");
            return View(userlogins.ToList());
        }

        //
        // GET: /UserLogin/Details/5

        public ActionResult Details(int id = 0)
        {
            UserLogin userlogin = db.UserLogins.Find(id);
            if (userlogin == null)
            {
                return HttpNotFound();
            }
            return View(userlogin);
        }

        //
        // GET: /UserLogin/Create

        public ActionResult Create()
        {
            ViewBag.EmId = new SelectList(db.CFEmployees, "EmId", "FirstName_Th");
            return View();
        }

        //
        // POST: /UserLogin/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(UserLogin userlogin)
        {
            if (ModelState.IsValid)
            {
                userlogin.RecordStatus = "U";
                userlogin.CreateBy = Session["LogedId"].ToString();
                userlogin.CreateWhen = DateTime.Now;
                db.UserLogins.Add(userlogin);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.EmId = new SelectList(db.CFEmployees, "EmId", "FirstName_Th", userlogin.EmId);
            return View(userlogin);
        }

        //
        // GET: /UserLogin/Edit/5

        public ActionResult Edit(int id = 0)
        {
            UserLogin userlogin = db.UserLogins.Find(id);
            if (userlogin == null)
            {
                return HttpNotFound();
            }
            ViewBag.EmId = new SelectList(db.CFEmployees, "EmId", "FirstName_Th", userlogin.EmId);
            return View(userlogin);
        }

        //
        // POST: /UserLogin/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(UserLogin userlogin)
        {
            if (ModelState.IsValid)
            {
                db.Entry(userlogin).State = EntityState.Modified;
                userlogin.UpdateBy = Session["LogedId"].ToString();
                userlogin.UpdateWhen = DateTime.Now;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.EmId = new SelectList(db.CFEmployees, "EmId", "FirstName_Th", userlogin.EmId);
            return View(userlogin);
        }

        //
        // GET: /UserLogin/Delete/5

        public ActionResult Delete(int id = 0)
        {
            UserLogin userlogin = db.UserLogins.Find(id);
            if (userlogin == null)
            {
                return HttpNotFound();
            }
            return View(userlogin);
        }

        //
        // POST: /UserLogin/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            UserLogin userlogin = db.UserLogins.Find(id);
            db.Entry(userlogin).State = EntityState.Modified;
            userlogin.RecordStatus = "D";
            userlogin.UpdateBy = Session["LogedId"].ToString();
            userlogin.UpdateWhen = DateTime.Now;
            //db.UserLogins.Remove(userlogin);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}